﻿using System.Collections.Generic;
using System.Linq;
using DotNet.HowToUseInterfacesDemo.GoodUse.Models;
using DotNet.HowToUseInterfacesDemo.GoodUse.Repositories;

namespace DotNet.HowToUseInterfacesDemo.GoodUse.Services
{
    /// <summary>
    /// This is the messanger service. It's the heart of little application
    /// Notice how the dependencies are injected in to the class via the constuctor.
    /// There are no references to implementations in this class.
    /// </summary>
    public class MessengerService : IMessengerService
    {
        public IPersonRepository PersonRepository { get; set; }
        protected List<IMessengerSenderService> MessengerSenderServices { get; set; }

        public MessengerService(IPersonRepository personRepository, IEnumerable<IMessengerSenderService> messengerSenderServices)
        {
            PersonRepository = personRepository;
            MessengerSenderServices = messengerSenderServices.ToList();
        }

        public void SendMessage(string message)
        {
            var people = PersonRepository.GetAll();
            people.ForEach(p => ContactPerson(p, message));
        }

        public void ContactPerson(Person person, string message)
        {
            MessengerSenderServices.ForEach(i => i.SendMessageToPerson(person, message));
        }
    }

    /// <summary>
    /// An interface to the messenger service, notice how only one method is availble
    /// </summary>
    public interface IMessengerService 
    {
        void SendMessage(string message);
    }

    /// <summary>
    /// I like to define contracts like this in the same file as the class that's mainly using it. 
    /// So it's some what clear that this interface is intended to only be used with this class. 
    /// </summary>
    public interface IMessengerSenderService
    {
        void SendMessageToPerson(Person person, string message);
    }
}
